package com.xiaodou.mjf.web.xface.basic.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.xiaodou.mjf.infrastructure.payeco.PayUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.xiaodou.mjf.application.basic.utils.FeeUtils;
import com.xiaodou.mjf.application.content.service.BannerService;
import com.xiaodou.mjf.application.fee.manager.FeeManager;
import com.xiaodou.mjf.infrastructure.common.controller.ApiController;
import com.xiaodou.mjf.infrastructure.common.utils.Global;
import com.xiaodou.mjf.web.xface.basic.domain.Banner;

/**
 * Created by Rocky on 2/26/15.
 */
@RestController
@RequestMapping("v1/basic/banner")
public class BannerController extends ApiController {

    @Resource
    BannerService bannerService;

    @RequestMapping("")
    @ResponseBody
    public Map<String, Object> list(Integer type) {

        //不填默认取首页banner，填写之后取对应产品banner 测试用
        List<Banner> banners = new ArrayList<Banner>();
        com.xiaodou.mjf.entity.content.Banner pram = new com.xiaodou.mjf.entity.content.Banner();
        if(type==null){
        	type=com.xiaodou.mjf.entity.content.Banner.TYPE_HOME;
        }
        pram.setType(type);
        pram.setStatus(com.xiaodou.mjf.entity.content.Banner.NORMAL);
        
        List<com.xiaodou.mjf.entity.content.Banner> list = bannerService.list(pram);
        /*
         * 广告id id 广告图片的地址 image 广告详情的链接 url 产品编号 product
		 */
        for (com.xiaodou.mjf.entity.content.Banner banner : list) {
            Banner entity = new Banner(banner.getId(), Global.getImgPath() + banner.getImage(), banner.getUrl(), banner.getFinancingId(),banner.getProductType());
            banners.add(entity);
        }

        Map<String, Object> result = success();
        result.put("items", banners);

        return result;
    }


    //todo:测试临时用的,上线前删除  费用计算
    @RequestMapping("ttest")
    @ResponseBody
    public Map<String, Object> ttest(String channelCode, String serveCode, double fee) {

        double fee2 = FeeUtils.calcFee(channelCode, serveCode, fee);

        Map<String, Object> result = success();
        result.put("fee", fee2);

        return result;
    }

    //todo:测试临时用的,上线前删除 满标计算
    @RequestMapping("ttest2")
    @ResponseBody
    public Map<String, Object> ttest2(String financingId) {

        FeeManager feeManager = new FeeManager();
        feeManager.calcFullScale(financingId);

        Map<String, Object> result = success();

        return result;
    }

    //todo:测试临时用的,上线前删除 满标付款
    @RequestMapping("ttest3")
    @ResponseBody
    public Map<String, Object> ttest3(String loanId) {

        FeeManager feeManager = new FeeManager();
        feeManager.payFullScale(loanId);

        Map<String, Object> result = success();

        return result;
    }

    //todo:测试临时用的,上线前删除 账单生成
    @RequestMapping("ttest4")
    @ResponseBody
    public Map<String, Object> ttest4(String loanId) {

        FeeManager feeManager = new FeeManager();
        feeManager.genOrder();

        Map<String, Object> result = success();

        return result;
    }

    //todo:测试临时用的,上线前删除 账单生成
    @RequestMapping("ttest5")
    @ResponseBody
    public Map<String, Object> ttest5(String memberId, int year, int month, double repaymentAmount) {

        logger.info("enter ttest5");
        FeeManager feeManager = new FeeManager();
        feeManager.payback(memberId, year, month, repaymentAmount);

        Map<String, Object> result = success();

        return result;
    }

    @RequestMapping("pay")
    public Map<String,Object> paytest(){
        try {
            //PayUtils.pay("RZJR201504130001","6222021001047526857","谭文科",100D,"工商银行","0","2015041300001");
            PayUtils.payQuery("RZJR201504130001", "2015041300001");
        }catch (Exception e){
            e.printStackTrace();
        }
        return map(true);
    }
}
